package cn.gov.anhai.controller;

import cn.gov.anhai.filter.Auth;
import cn.gov.anhai.mappers.PublicMapper;
import cn.gov.anhai.service.DynamicService;
import cn.gov.anhai.service.HomeService;
import cn.gov.anhai.service.PublicService;
import cn.gov.anhai.service.TourismService;
import cn.gov.anhai.utils.ApiResult;
import cn.gov.anhai.utils.LoginUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 动态
 */
@RestController
@Api(tags = {"动态"})
public class DynamicController {
    private Logger logger = LoggerFactory.getLogger(HomeController.class);

    @Resource
    PublicService publicService;

    @Resource
    PublicMapper publicMapper;

    @Resource
    DynamicService dynamicService;

    @Resource
    TourismService tourismService;

    /**
     * 动态列表
     */
    @GetMapping(value = "/dynamic/list")
    @ApiOperation(value = "动态列表")
    public ApiResult<Object> dynamicList(Integer current, Integer size, HttpServletRequest request) throws Exception {
        Map<String, Object> result = new HashMap<>();
        List<Map<String, Object>> records = dynamicService.list(current, size, null, null, null, 0, 2, "desc");
        Integer total = dynamicService.listCount(null, null, null, 0, 2);
        result.put("records", records);
        result.put("total", total);
        return ApiResult.ok(result);
    }

    /**
     * 动态详情
     */
    @GetMapping(value = "/dynamic/{id}")
    @ApiOperation(value = "动态详情")
    public ApiResult<Object> dynamicInfo(@PathVariable("id") Long id, String isPrivate, HttpServletRequest request) throws Exception {
        Map<String, Object> info = dynamicService.info(id, LoginUtil.getToken(request), isPrivate);
        if (info == null) return ApiResult.fail("查无动态信息");
        return ApiResult.ok(info);
    }

    /**
     * 待修改的动态详情
     */
    @Auth
    @GetMapping(value = "/publish/dynamic/{id}")
    @ApiOperation(value = "待修改的动态详情")
    public ApiResult<Object> dynamicInfoByUserId(@PathVariable("id") Long id, HttpServletRequest request) throws Exception {
        Map<String, Object> info = dynamicService.infoByUserId(id, LoginUtil.getToken(request));
        if (info == null) return ApiResult.fail("查无修改动态信息");
        return ApiResult.ok(info);
    }

    /**
     * 收藏动态信息
     */
    @Auth
    @PostMapping(value = "/dynamic/collect/{id}")
    @ApiOperation(value = "收藏动态信息")
    public ApiResult<Object> doCollect(@PathVariable("id") Long id, HttpServletRequest request) throws Exception {
        Map<String, Object> result = dynamicService.doCollect(LoginUtil.getToken(request), id);
        if (result == null) return ApiResult.fail("操作失败");
        return ApiResult.ok(result);
    }

    /**
     * 点赞动态信息
     */
    @Auth
    @PostMapping(value = "/dynamic/like/{id}")
    @ApiOperation(value = "点赞动态信息")
    public ApiResult<Object> doLike(@PathVariable("id") Long id, HttpServletRequest request) throws Exception {
        Map<String, Object> result = dynamicService.doLike(LoginUtil.getToken(request), id);
        if (result == null) return ApiResult.fail("操作失败");
        return ApiResult.ok(result);
    }


    /**
     * 打卡点列表
     */
    @GetMapping(value = "/publish/location/list")
    @ApiOperation(value = "打卡点列表")
    public ApiResult<Object> searchList(Integer current, Integer size, Long type, String name, HttpServletRequest request) throws Exception {
        Map<String, Object> result = new HashMap<>();
        List<Map<String, Object>> records = tourismService.list(current, size, type, name, null, null, null);
        Integer total = tourismService.listCount(type, name, null, null, null);
        result.put("records", records);
        result.put("total", total);
        return ApiResult.ok(result);
    }

    /**
     * 打卡点详情
     */
    @GetMapping(value = {"/publish/location/{id}"})
    @ApiOperation(value = "打卡点详情")
    public ApiResult<Object> info(@PathVariable("id") Long id, HttpServletRequest request) throws Exception {
        Map<String, Object> info = tourismService.info(id, LoginUtil.getToken(request));
        if (info == null) return ApiResult.fail("查无信息");

        Map<String, Object> data = new HashMap<>();
        data.put("id", info.get("id"));
        data.put("name", info.get("title"));
        return ApiResult.ok(data);
    }

    /**
     * 发布动态
     */
    @Auth
    @PostMapping(value = "/publish/post")
    @ApiOperation(value = "发布动态")
    public ApiResult<Object> add(@RequestBody JSONObject object, HttpServletRequest request) throws Exception {
        String title = object.getStr("title");
        String content = object.getStr("content");
        JSONArray locationList = object.getJSONArray("locationList");
        JSONArray photos = object.getJSONArray("photos");
        JSONObject size = object.getJSONObject("coverSize");
        JSONObject video = object.getJSONObject("video");

        return dynamicService.add(LoginUtil.getToken(request), title, content, locationList, size, photos, video);
    }

    /**
     * 修改动态
     */
    @Auth
    @PutMapping(value = "/publish/post")
    @ApiOperation(value = "修改动态")
    public ApiResult<Object> update(@RequestBody JSONObject object, HttpServletRequest request) throws Exception {
        Long id = object.getLong("id");
        String title = object.getStr("title");
        String content = object.getStr("content");
        JSONArray locationList = object.getJSONArray("locationList");
        JSONArray photos = object.getJSONArray("photos");
        JSONObject size = object.getJSONObject("coverSize");
        JSONObject video = object.getJSONObject("video");

        return dynamicService.update(id, LoginUtil.getToken(request), title, content, locationList, size, photos, video);
    }

    /**
     * 删除动态
     */
    @Auth
    @DeleteMapping(value = "/publish/post/{id}")
    @ApiOperation(value = "删除动态")
    public ApiResult<Object> delete(@PathVariable("id") Long id, HttpServletRequest request) throws Exception {
        return dynamicService.delete(id, LoginUtil.getToken(request));
    }
}
